var app = getApp();
import {
  api,
  util,
  dialog,
  forms,
  storage
} from '../../../js/util'
import {
  router
} from '../../../js/router';
import {
  coupon
} from '../../../js/modules/market.js'
import authorize from '../../../js/modules/authorize.js'
const componentBehavior = require('../../../js/common/ComponentBehavior')

Component({
  behaviors: [componentBehavior],
  /**
   * 组件的属性列表
   */
  properties: {
    pageKey: {
      type: String,
      value: "index"
    }
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {
    },
    hide() {}
  },
  /**
   * 组件的初始数据
   */
  data: {
    item: {},
    list: [],
    gift_key: "gift_key",
    isShow: false,
    isReceive:false,
    loginType: 0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getCouponGift(loginType) {
      var that = this;
      var obj = storage.get(that.data.gift_key);
      // console.log(obj,'======obj');
      coupon.getCouponGift({}, function(content) {        
        var isShow = false,
          item = {
            id: 0,
            clickNum:0,
            scanNum:0,
            title: "新人专享大礼包",
            btnName: "立即领取",
            list: []
          };
        if (content && content.length > 0) {
          item.id = content[0].Id;
          item.type = content[0].Type;
          item.list = content[0].Coupons;
          item.showNum=0;
        }
        else{
          that.setData({
            isShow: false
          });
          return;
        }
        if ((obj == null || obj == "") && item != null) {
          isShow = true;
        } else if (obj != null && (item.id != obj.id) || (obj.showNum || 1) < 3) {
          isShow = true;
          item.showNum = obj.showNum || 1;
        }
        if (item.type == 1) {
          item.btnName = "立即注册";
          item.title = "注册送优惠券"
        }
        item.isShow = true;
        if (!isShow){
          item = obj;
        }          

        that.setData({
          isShow: isShow,
          item: item,
          loginType: loginType
        });       
        if (isShow){
          item.scanNum = item.scanNum + 1;
          item.showNum = item.showNum+1;
          // storage.put(that.data.gift_key, item);
          that.updateScanNum(item,1);
        }
        if (!obj && obj.isShow && obj.clickNum > 0) {
          //直接领取
          //that.receiveCouponGift();
          return;
        }
      });
    },
    /**
     * 领取大礼包
     */
    receiveCouponGift(e) {
      if (this.data.isReceive){
        return;
      }
      var that = this;
      var item = this.data.item;
      var id = item.id;
      var type = item.type;
      this.setData({
        isReceive: true
      }, () => {
        coupon.receiveCouponGift({ couponGiftId: id, type: type }, function (res) {
          if (res.Success) {
            util.tips("领取成功");
            item.isReceived = true;
            storage.put(that.data.gift_key, item);
          }
          else {
            util.tips(res.Message);
          }
          // item.isReceived = true;
          // storage.put(that.data.gift_key, item);
          that.onCloseGiftPacks();
        });
      })
    },
    /**
     * 更新浏览数量
     */
    updateScanNum(item,t){
      var that = this;
      var id = item.id;
      var type = item.type;

      coupon.updateScanNum({ couponGiftId: id, type: t,count:t==1?item.scanNum:item.clickNum },function(){

      });
    },
    /**
     * 单击领取
     */
    onClickGiftPacks(e) {
      var that=this;
      var item = this.data.item;
      var loginType = this.data.loginType;
      //如果登录成功直接领取
      if (authorize.isLogin('store')) {
        item.clickNum = item.clickNum+1;
        storage.put(that.data.gift_key, item);
        that.updateScanNum(item,2);
        this.receiveCouponGift(e);
        return;
      } else {
        if (loginType != 2 && item.type == 1) {
          //跳转到注册页面
          router.redirect({
            path: "register",
            params: {
              role: "store"
            }
          });
        } else {
          //跳转到登录页面
          router.redirect({
            path: "login",
            params: {
              role: "store"
            }
          });
        }
      }

    },
    onCloseGiftPacks(e) {
      var that=this
      // storage.put(that.data.gift_key,'');
      this.setData({
        isShow: false
      },()=>{
        that.setData({
          isReceive: false
        })
      });
      
    }
  }
})